package gold.contest;


import gold.utils.InputUtil;

import java.util.*;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC5630 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public int maximumUniqueSubarray(int[] nums) {
            int[] preSum = new int[nums.length + 1];
            for (int i = 0; i < nums.length; i++) {
                preSum[i + 1] = preSum[i] + nums[i];
            }

            int curMaxScore = Integer.MIN_VALUE, curScore = 0, left = 0, right = 0;
            Map<Integer, Integer> dict = new HashMap<>();
            while (right < nums.length) {
                if (dict.containsKey(nums[right])) {
                    //当前窗口需要缩小
                    left = Math.max(left, dict.get(nums[right]) + 1);
                    curScore = preSum[right + 1] - preSum[left];
                } else {
                    curScore += nums[right];
                }

                dict.put(nums[right], right);//更新位置
                right++;
                curMaxScore = Math.max(curMaxScore, curScore);
            }

            return curMaxScore;
        }
    }

    public void run() {
        Solution solution = new Solution();
        int[] arr = InputUtil.toIntegerArray("[4,2,4,5,6]");
        System.out.println(solution.maximumUniqueSubarray(arr));
    }

    public static void main(String[] args) throws Exception {
        LC5630 an = new LC5630();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
